import puppeteer, { HTTPRequest, LaunchOptions } from 'puppeteer';
import * as path from 'path';
import * as fs from 'fs';
import { onRequest, onResponse } from './tool/on';
import { fetchVideoMeta } from './tool/videometa';
import { fetchNewAts } from './tool/at';
import { fetchNote, fetchNoteWithAt } from './tool/note';

const lauchOptions: LaunchOptions = {
    channel: undefined,
    executablePath: undefined,
    ignoreDefaultArgs: false,
    enableExtensions: false,
    handleSIGINT: true,
    handleSIGTERM: true,
    handleSIGHUP: true,
    timeout: 0,
    dumpio: undefined,
    env: undefined,
    pipe: undefined,
    browser: undefined,
    extraPrefsFirefox: undefined,
    waitForInitialPage: undefined,
    headless: false,
    userDataDir: path.join(process.cwd(), 'browser-data'),
    devtools: false,
    debuggingPort: undefined,
    args: undefined,

    // ConnectOptions
    acceptInsecureCerts: undefined,
    networkEnabled: undefined,
    downloadBehavior: undefined,
    slowMo: undefined,
    targetFilter: undefined,
    protocol: undefined,
    protocolTimeout: undefined,
    browserWSEndpoint: undefined,
    browserURL: undefined,
    transport: undefined,
    headers: undefined,
    capabilities: undefined
};

https://api.bilibili.com/x/v2/reply/wbi/main?oid=778925870&type=1&mode=3&pagination_str=%7B%22offset%22:%22%22%7D&plat=1&seek_rpid=&web_location=1315875&w_rid=6df0879dedd5d066f78ad95c2f4b5109&wts=1758014669

(async () => {
    const browser = await puppeteer.launch(lauchOptions);
    const page = await browser.newPage();

    const tempPath = path.join(process.cwd(), 'temp', `${Date.now()}`);
    if (!fs.existsSync(tempPath)) {
        fs.mkdirSync(tempPath, { recursive: true });
    }

    await page.goto('https://www.bilibili.com');
    // page.on('request', onRequest(tempPath));
    // page.on('response', onResponse(tempPath));

    // const ats = await fetchNewAts(page);
    // console.log(ats);

    // const page2 = await browser.newPage();
    // await page2.setViewport(viewPort);
    // const { at, note } = await fetchNoteWithAt(page2, ats[0])!;
    // await page2.close();
    // console.log(at, note);

    // const BVid = 'BV1my4y1D7zk';
    // const videoMeta = await fetchVideoMeta(page, BVid);
    // console.log(videoMeta);
})();
